import {
    specslist,
    specscount
} from '../../../request/app'

const state = {
    list: [],
    sizepage: {
        size: 3,
        page: 1
    },
    pages: 0
}
const mutations = {
    MUsetlist(state, payload) {
        state.list = payload
    },
    Musetpages(state, payload) {
        state.pages = payload
    }
}
const actions = {
    ACsetlist(content) {
        specslist(content.state.sizepage)
            .then(res => {
                if (content.state.sizepage != 1 && res.list.length == 0) {
                    content.dispatch('ACsetpage', content.state.sizepage.page - 1)
                    content.dispatch('ACgetpage')
                }
                content.commit('MUsetlist', res.list)
            })
    },
    ACsetpage(content, payload) {
        // console.log(payload);
        // console.log(content);
        content.state.sizepage.page = payload
        specslist(content.state.sizepage)
            .then(res => {
                // console.log(res);
                content.commit('MUsetlist', res.list)
            })
    },
    ACgetpage({
        commit
    }) {
        specscount().then(res => {
            commit('Musetpages', res.list[0].total)
        })
    }


}
const getters = {
    getlist(state) {
        return state.list
    },
    getpages(state) {
        return state.pages
    }

}

export default {
    state,
    mutations,
    actions,
    getters,
    namespaced: true
}